package com.jinandi.app.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.jinandi.app.domain.Authorit;
import com.jinandi.app.service.AuthoritService;

/**
 * <角色管理>
 * @author CGC
 * @version [记录版本号, 2013-4-26]
 */
@Controller
@RequestMapping("/admin")
public class AuthoritController  extends BaseController{
	
	private AuthoritService authoritService;
	
	@Autowired
	public void setAuthoritService(AuthoritService authoritService) {
		this.authoritService = authoritService;
	}
	
	/**
	 * <删除角色>
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-5-2 上午10:27:26
	 */
	@RequestMapping(value = "/authorit/delauthorit.json")
	public void delauthorit(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			authoritService.deleteAuthoritById(Long.valueOf(map.get("id")));
			 modelMap.addAttribute("Result", "OK");
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}
	
	/**
	 * <修改角色>
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-5-2 上午10:23:37
	 */
	@RequestMapping(value = "/authorit/modifyauthorit.json", method = RequestMethod.POST)
	public void modifyauthorit(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			 Authorit authorit = authoritService.findAuthoritById(Long.valueOf(map.get("id")));
			 authorit.setAuName(map.get("auName"));
			 authorit.setAuthority(map.get("authority"));
			 authoritService.saveAuthorit(authorit);
			 modelMap.addAttribute("Result", "OK");
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}
	
	/**
	 * <新增角色>
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-5-2 上午10:17:44
	 */
	@RequestMapping(value = "/authorit/createauthorit.json", method = RequestMethod.POST)
	public void createauthorit(WebRequest request,ModelMap modelMap){
		Map<String ,String> map = getStrictParams(request.getParameterMap());
		 try {
			 Authorit authorit = new Authorit();
			 authorit.setAuName(map.get("auName"));
			 authorit.setAuthority(map.get("authority"));
			 authoritService.saveAuthorit(authorit);
			 modelMap.addAttribute("Result", "OK");
			 modelMap.addAttribute("Record", authorit);
	     }catch (Exception e){
	    	 e.printStackTrace();
	    	 modelMap.addAttribute("Result", "ERROR");
	    	 modelMap.addAttribute("Message", e.getMessage());
	     }
	}

	/**
	 * <列表的json数据>
	 * @param request
	 * @param modelMap
	 * @author CGC 
	 * 创建日期:2013-4-26 下午2:49:19
	 */
	@RequestMapping(value = "/authorit/authorit.json", method = RequestMethod.POST)
	public void getAuthoritJson(WebRequest request, ModelMap modelMap) {
		Map<String, String> map = getStrictParams(request.getParameterMap());
		Map<String,Object> params = new HashMap<String,Object>();
		if(map.containsKey("auName")){
			params.put("auName", map.get("auName"));
		}
		if(map.containsKey("authority")){
			params.put("authority", map.get("authority"));
		}
		Page<Authorit> findUser = authoritService.findAuthoritLisr(params,map);
		getFormatJsonForJtable(modelMap, findUser);
	}
	
}
